package com.lz.yz.reward.web;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.reward.entity.CrowdfundingShare;
import com.lz.yz.reward.model.CrowdfundingShareModel;
import com.lz.yz.reward.model.RewardModel;
import com.lz.yz.reward.param.RewardParam;
import com.lz.yz.reward.service.CrowdfundingShareService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* Created by CodeGenerator on 2017/11/24.
*/
@RestController
public class CrowdfundingShareController {
    @Resource
    private CrowdfundingShareService crowdfundingShareService;


    @Autowired
    HttpServletRequest request;

    @ApiOperation(value="分享增加积分")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "model", value = "分享实体类", dataType = "CrowdfundingShareModel", paramType = "body"),
    })
    @PostMapping("/crowdfunding/share")
    public ReturnValue add(@RequestBody CrowdfundingShareModel model) {
        Integer accountId = (Integer) request.getAttribute("accountId");
        model.setTo(accountId);
        return crowdfundingShareService.updateCrowdfundingShare(model);
    }



    @ApiOperation(value="生成分享卡",response = RewardModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "path",name = "rewardId", value = "悬赏id"),
            @ApiImplicitParam(paramType = "query",name = "url", value = "悬赏分享的url"),
    })
    @GetMapping("/reward/shareCard/{rewardId}")
    public ReturnValue shareCard(@PathVariable Integer rewardId,String url) {
        if(url==null){
            return ReturnValue.generateFalseReturnValue("url无效!");
        }
        Integer accountId = (Integer) request.getAttribute("accountId");
        RewardParam param=new RewardParam();
        param.setRewardId(rewardId);
        param.setShareUrl(url);
        param.setLoginId(accountId);
        return crowdfundingShareService.createShareCard(param);
    }
}
